package cn.ihelloworld.controller;

import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ihelloworld.bean.Catagory;
import cn.ihelloworld.service.ICatagoryService;
import cn.ihelloworld.util.JSONResult;

@Controller
@RequestMapping("/admin/catagory/")
public class CatagoryController {
	@Autowired
	private ICatagoryService catagoryService;
	
	@RequestMapping("index")
	public String index(){
		return "admin/catagory";
	}
	
	@RequestMapping("query")
	@ResponseBody
	public JSONResult<Catagory> query(Catagory catagory){
		JSONResult<Catagory> json=new JSONResult<Catagory>();
		List<Catagory> list=catagoryService.select(catagory);
		json.setRows(list);
		return json;
	}
	
	@RequestMapping("add")
	@ResponseBody
	public JSONResult<Catagory> modify(String list){
		JSONResult<Catagory> json=new JSONResult<Catagory>();
		System.out.println(list);
		JSONArray jsonarray=JSONArray.fromObject(list);
		List<Catagory> catagorys=JSONArray.toList(jsonarray,Catagory.class);
		catagoryService.saveOrUpdate(catagorys);
		json.setStatus(true);
		return json;
	}
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult<Catagory> delete(String ids){
		JSONResult<Catagory> json=new JSONResult<Catagory>();
		String[] array=ids.split(",");
		catagoryService.delete(array);
		json.setStatus(true);
		return json;
	}
}
